home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / lib2to3 / fixes / fix_idioms.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  4.1 KB  |  103 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''Adjust some old Python 2 idioms to their modern counterparts.
  5.  
  6. * Change some type comparisons to isinstance() calls:
  7.     type(x) == T -> isinstance(x, T)
  8.     type(x) is T -> isinstance(x, T)
  9.     type(x) != T -> not isinstance(x, T)
  10.     type(x) is not T -> not isinstance(x, T)
  11.  
  12. * Change "while 1:" into "while True:".
  13.  
  14. * Change both
  15.  
  16.     v = list(EXPR)
  17.     v.sort()
  18.     foo(v)
  19.  
  20. and the more general
  21.  
  22.     v = EXPR
  23.     v.sort()
  24.     foo(v)
  25.  
  26. into
  27.  
  28.     v = sorted(EXPR)
  29.     foo(v)
  30. '''
  31. from  import fixer_base
  32. from fixer_util import Call, Comma, Name, Node, syms
  33. CMP = "(n='!=' | '==' | 'is' | n=comp_op< 'is' 'not' >)"
  34. TYPE = "power< 'type' trailer< '(' x=any ')' > >"
  35.  
  36. class FixIdioms(fixer_base.BaseFix):
  37.     explicit = True
  38.     PATTERN = "\n        isinstance=comparison< %s %s T=any >\n        |\n        isinstance=comparison< T=any %s %s >\n        |\n        while_stmt< 'while' while='1' ':' any+ >\n        |\n        sorted=any<\n            any*\n            simple_stmt<\n              expr_stmt< id1=any '='\n                         power< list='list' trailer< '(' (not arglist<any+>) any ')' > >\n              >\n              '\\n'\n            >\n            sort=\n            simple_stmt<\n              power< id2=any\n                     trailer< '.' 'sort' > trailer< '(' ')' >\n              >\n              '\\n'\n            >\n            next=any*\n        >\n        |\n        sorted=any<\n            any*\n            simple_stmt< expr_stmt< id1=any '=' expr=any > '\\n' >\n            sort=\n            simple_stmt<\n              power< id2=any\n                     trailer< '.' 'sort' > trailer< '(' ')' >\n              >\n              '\\n'\n            >\n            next=any*\n        >\n    " % (TYPE, CMP, CMP, TYPE)
  39.     
  40.     def match(self, node):
  41.         r = super(FixIdioms, self).match(node)
  42.         if r and 'sorted' in r:
  43.             if r['id1'] == r['id2']:
  44.                 return r
  45.             return None
  46.         return r
  47.  
  48.     
  49.     def transform(self, node, results):
  50.         if 'isinstance' in results:
  51.             return self.transform_isinstance(node, results)
  52.         if 'while' in results:
  53.             return self.transform_while(node, results)
  54.         if 'sorted' in results:
  55.             return self.transform_sort(node, results)
  56.         raise RuntimeError('Invalid match')
  57.  
  58.     
  59.     def transform_isinstance(self, node, results):
  60.         x = results['x'].clone()
  61.         T = results['T'].clone()
  62.         x.set_prefix('')
  63.         T.set_prefix(' ')
  64.         test = Call(Name('isinstance'), [
  65.             x,
  66.             Comma(),
  67.             T])
  68.         if 'n' in results:
  69.             test.set_prefix(' ')
  70.             test = Node(syms.not_test, [
  71.                 Name('not'),
  72.                 test])
  73.         
  74.         test.set_prefix(node.get_prefix())
  75.         return test
  76.  
  77.     
  78.     def transform_while(self, node, results):
  79.         one = results['while']
  80.         one.replace(Name('True', prefix = one.get_prefix()))
  81.  
  82.     
  83.     def transform_sort(self, node, results):
  84.         sort_stmt = results['sort']
  85.         next_stmt = results['next']
  86.         list_call = results.get('list')
  87.         simple_expr = results.get('expr')
  88.         if list_call:
  89.             list_call.replace(Name('sorted', prefix = list_call.get_prefix()))
  90.         elif simple_expr:
  91.             new = simple_expr.clone()
  92.             new.set_prefix('')
  93.             simple_expr.replace(Call(Name('sorted'), [
  94.                 new], prefix = simple_expr.get_prefix()))
  95.         else:
  96.             raise RuntimeError('should not have reached here')
  97.         list_call.remove()
  98.         if next_stmt:
  99.             next_stmt[0].set_prefix(sort_stmt.get_prefix())
  100.         
  101.  
  102.  
  103.